<?php

class Default_Model_Sach extends Zend_Db_Table_Abstract {

    public $_name = 'sach';
    public $_primary = 'masach';

    public function getTheLoai1($id) {
        $se = $this->select();
        $se->where("matheloai1=?", $id);
        return $this->fetchAll($se)->toArray();
    }

    public function getTheLoai1_2($id1, $id2) {
        $se = $this->select();
        $se->where("matheloai1=? and matheloai2=?", $id1, $id2);
        return $this->fetchAll($se)->toArray();
    }

    public function getTheLoai1_2_3($id1, $id2, $id3) {
        $se = $this->select();
        $se->where("matheloai1=? and matheloai2=? and matheloai3=?", $id1, $id2, $id3);
        return $this->fetchAll($se)->toArray();
    }

    public function getSachId($id) {
        $se = $this->select();
        $se->where("masach=?", $id);
        return $this->fetchAll($se)->toArray();
    }

//begin khuyen mai
    public function getSachKhuyenMai() {
        $se = $this->select();
        $se->where("giamgia>0");
        return $this->fetchAll($se)->toArray();
    }

    public function getSach4KhuyenMai($page) {
        $se = $this->select();
        $se->where("giamgia>0");
        $se->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function page4KhuyenMai() {
        $se = $this->select();
        $se->where("giamgia>0");
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

    public function getKhuyenMaiTheLoai1($id) {
        $se = $this->select();
        $se->where("giamgia>0 and matheloai1=?", $id);
        return $this->fetchAll($se)->toArray();
    }

    public function get4KhuyenMaiTheLoai1($id, $page) {
        $se = $this->select();
        $se->where("giamgia>0 and matheloai1=?", $id);
        $se->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function page4KhuyenMaiTheLoai1($id) {
        $se = $this->select();
        $se->where("giamgia>0 and matheloai1=?", $id);
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

    public function getSachKhuyenMaiTheLoai1_2($id1, $id2) {
        $se = $this->select();
        $se->where("giamgia>0 and matheloai1=? and matheloai2=?", $id1, $id2);
        return $this->fetchAll($se)->toArray();
    }

    public function get4KhuyenMaiTheLoai1_2($id1, $id2, $page) {
        $se = $this->select();
        $se->where("giamgia>0 and matheloai1=? and matheloai2=?", $id1, $id2);
        $se->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function page4KhuyenMaiTheLoai1_2($id1, $id2) {
        $se = $this->select();
        $se->where("giamgia>0 and matheloai1=? and matheloai2=?", $id1, $id2);
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

    public function getSachKhuyenMaiTheLoai1_2_3($id1, $id2, $id3) {
        $se = $this->select();
        $se->where("giamgia>0 and matheloai1=? and matheloai2=? and matheloai3=?", $id1, $id2, $id3);
        return $this->fetchAll($se)->toArray();
    }

    public function get4KhuyenMaiTheLoai1_2_3($id1, $id2, $id3, $page) {
        $se = $this->select();
        $se->where("giamgia>0 and matheloai1=? and matheloai2=? and matheloai3=?", $id1, $id2, $id3);
        $se->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function page4KhuyenMaiTheLoai1_2_3($id1, $id2, $id3) {
        $se = $this->select();
        $se->where("giamgia>0 and matheloai1=? and matheloai2=? and matheloai3=?", $id1, $id2, $id3);
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

//end khuyen main
    //
    //begin ban chay
    public function getSach4BanChay($page) {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=1')
                ->order('sum(cthd.soluong) desc')
                ->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function getSachBanChay() {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc');
        return $this->fetchAll($se)->toArray();
    }

    public function page4BanChay() {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc');
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

    public function getSachBanChayTheLoai1($id) {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->where("sach.matheloai1=?", $id)
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc');
        return $this->fetchAll($se)->toArray();
    }

    public function getSach4BanChayTheLoai1($id, $page) {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->where("sach.matheloai1=?", $id)
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc')
                ->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function page4BanChayTheLoai1($id) {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->where("sach.matheloai1=?", $id)
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc');
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

    public function getSachBanChayTheLoai1_2($id1, $id2) {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->where("sach.matheloai1=? and sach.matheloai2=?", $id1, $id2)
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc');
        return $this->fetchAll($se)->toArray();
    }

    public function getSach4BanChayTheLoai1_2($id1, $id2, $page) {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->where("sach.matheloai1=? and sach.matheloai2=?", $id1, $id2)
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc')
                ->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function page4BanChayTheLoai1_2($id1, $id2) {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->where("sach.matheloai1=? and sach.matheloai2=?", $id1, $id2)
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc');
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

    public function getSachBanChayTheLoai1_2_3($id1, $id2, $id3) {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->where("sach.matheloai1=? and sach.matheloai2=? and sach.matheloai3=?", $id1, $id2, $id3)
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc');
        return $this->fetchAll($se)->toArray();
    }

    public function getSach4BanChayTheLoai1_2_3($id1, $id2, $id3, $page) {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->where("sach.matheloai1=? and sach.matheloai2=? and sach.matheloai3=?", $id1, $id2, $id3)
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc')
                ->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function page4BanChayTheLoai1_2_3($id1, $id2, $id3) {
        $se = $this->select(SELECT_WITH_FROM_PART)
                ->setIntegrityCheck(false)
                ->join('cthd', 'sach.masach=cthd.masach')
                ->where("sach.matheloai1=? and sach.matheloai2=? and sach.matheloai3=?", $id1, $id2, $id3)
                ->group('cthd.masach')
                ->having('sum(cthd.soluong)>=2')
                ->order('sum(cthd.soluong) desc');
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

//end ban chay
    //
    //begin khac
    public function getSach4Khac($page) {
        $se = $this->select();
        $se->where("giamgia=0");
        $se->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function getSachKhac() {
        $se = $this->select();
        $se->where("giamgia=0");
        return $this->fetchAll($se)->toArray();
    }

    public function page4Khac() {
        $se = $this->select();
        $se->where("giamgia=0");
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

    public function getSachKhacTheLoai1($id1) {
        $se = $this->select();
        $se->where("giamgia=0 and sach.matheloai1=?", $id1);
        return $this->fetchAll($se)->toArray();
    }

    public function getSach4KhacTheLoai1($id1, $page) {
        $se = $this->select();
        $se->where("giamgia=0 and sach.matheloai1=?", $id1);
        $se->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function pageKhacTheLoai1($id1) {
        $se = $this->select();
        $se->where("giamgia=0 and sach.matheloai1=?", $id1);
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

    public function getSachKhacTheLoai1_2($id1, $id2) {
        $se = $this->select();
        $se->where("giamgia=0 and sach.matheloai1=? and sach.matheloai2=?", $id1, $id2);
        return $this->fetchAll($se)->toArray();
    }

    public function getSach4KhacTheLoai1_2($id1, $id2, $page) {
        $se = $this->select();
        $se->where("giamgia=0 and sach.matheloai1=? and sach.matheloai2=?", $id1, $id2);
        $se->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function pageKhacTheLoai1_2($id1, $id2) {
        $se = $this->select();
        $se->where("giamgia=0 and sach.matheloai1=? and sach.matheloai2=?", $id1, $id2);
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

    public function getSachKhacTheLoai1_2_3($id1, $id2, $id3) {
        $se = $this->select();
        $se->where("giamgia=0 and sach.matheloai1=? and sach.matheloai2=? and sach.matheloai3=?", $id1, $id2, $id3);
        return $this->fetchAll($se)->toArray();
    }

    public function getSach4KhacTheLoai1_2_3($id1, $id2, $id3, $page) {
        $se = $this->select();
        $se->where("giamgia=0 and sach.matheloai1=? and sach.matheloai2=? and sach.matheloai3=?", $id1, $id2, $id3);
        $se->limitPage($page, 4);
        return $this->fetchAll($se)->toArray();
    }

    public function pageKhacTheLoai1_2_3($id1, $id2, $id3) {
        $se = $this->select();
        $se->where("giamgia=0 and sach.matheloai1=? and sach.matheloai2=? and sach.matheloai3=?", $id1, $id2, $id3);
        $rows = $this->fetchAll($se)->toArray();
        $count = sizeof($rows);
        return $count % 4 == 0 ? (int) $count / 4 : (int) ($count / 4) + 1;
    }

}

